﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel.Composition.Primitives;

namespace Snowdreamist.Prism.Region
{
	public static class RegionHelper
	{
		/// <summary>
		/// 注册RegionAdapter
		/// </summary>
		/// <param name="registerAction">注册动作</param>
		/// <param name="regionAdapters">适配器</param>
		public static void RegisterRegionAdapter(Action<Type,IRegionAdapter> registerAction,IEnumerable<IRegionAdapter> regionAdapters)
		{
			if (registerAction == null)
				throw new ArgumentNullException("registerAction");
			if (regionAdapters == null)
				throw new ArgumentNullException("regionAdapters");

			foreach (var x in regionAdapters)
			{
				Type targetType = RegionHelper.GetRegionAdapterTargetType(x.GetType());
				registerAction(targetType, x);
			}
		}
		/// <summary>
		/// 获取RegionAdapter目标类型
		/// </summary>
		/// <param name="adapterType"></param>
		/// <returns></returns>
		public static Type GetRegionAdapterTargetType(Type adapterType)
		{
			object[] attrs = adapterType.GetCustomAttributes(typeof(RegionAdapterTargetTypeAttribute), true);
			if (attrs.Count() == 0)
				throw new ArgumentException("AdapterType is invalid!");
			return attrs.Cast<RegionAdapterTargetTypeAttribute>().First().TargetType;
		}
		/// <summary>
		/// 获得<paramref name="targetType"/>上ViewDeclareAttribute上定义的视图名称
		/// </summary>
		/// <param name="targetType">目标类型</param>
		/// <returns>视图名称</returns>
		public static string GetViewDeclareName(Type targetType)
		{
			object[] attrs = targetType.GetCustomAttributes(typeof(ViewDeclareAttribute), true);
			if (attrs.Count() == 0)
				return null;

			if (attrs.Count() > 1)
				throw new DuplicatedViewDeclareNameFoundedException(targetType.ToString());

			return (attrs.FirstOrDefault() as ViewDeclareAttribute).ViewName;
		}
		/// <summary>
		/// 获得导出特性定义，只支持第一个导出特性
		/// </summary>
		/// <param name="targetType">目标类型</param>
		/// <returns></returns>
		public static ExportAttribute GetExportAttribute(Type targetType)
		{
			object[] attrs = targetType.GetCustomAttributes(typeof(ExportAttribute), false);
			if (attrs.Count() == 0)
				return null;
			return attrs.First() as ExportAttribute;
		}
		/// <summary>
		/// 获得ViewPart
		/// </summary>
		/// <param name="export">导出</param>
		/// <returns></returns>
		internal static ViewPart GetViewPart(Export export)
		{
			if (export == null)
				throw new ArgumentNullException("exportDefinition");
			ViewPart viewPart = new ViewPart()
			{
				Value = export.Value,
				TargetViewName = export.Metadata[ViewComposeManager.TargetViewKey] as string,
				TargetRegionName = export.Metadata[ViewComposeManager.TargetRegionKey] as string,
				Order = export.Metadata.ContainsKey(ViewComposeManager.OrderKey) ? (int)export.Metadata[ViewComposeManager.OrderKey] : 0
			};
			return viewPart;
		}
	}
}
